Python Collections Quick Reference

Table Of Contents

  1. Deque
  2. Heapq
  3. Counter

1. Deque


In [1]:
from collections import deque

dq = deque()
dq.append(1)
dq.append(2)
dq.appendleft(3)
dq


Out[1]:
deque([3, 1, 2])

In [2]:
v = dq.pop()
v


Out[2]:
2

In [3]:
dq.popleft()


Out[3]:
3

In [4]:
dq


Out[4]:
deque([1])

Using maxlen to limit the num of items in a deque


In [5]:
dq = deque(maxlen = 3)
for n in range(10):
    dq.append(n)
dq


Out[5]:
deque([7, 8, 9])

2. Heapq

heapq provides O(1) access to the smallest item in the heap.


In [11]:
import heapq
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]

In [12]:
#heapq is created from a list

heap = list(nums)
heapq.heapify(heap)

#now the 1st element is guarenteed to be the smallest
heap


Out[12]:
[-4, 2, 1, 23, 7, 2, 18, 23, 42, 37, 8]

In [13]:
heapq.heappop(heap)


Out[13]:
-4

In [14]:
heap


Out[14]:
[1, 2, 2, 23, 7, 8, 18, 23, 42, 37]

In [15]:
heapq.heappush(heap, -10)

heap


Out[15]:
[-10, 1, 2, 23, 2, 8, 18, 23, 42, 37, 7]

nlargest / nsmallest wraps creation of a heap for one-time access


In [16]:
# nlargest and nsmallest wrap a heapq to provide its results
print(heapq.nlargest(3, nums)) # Prints [42, 37, 23]
print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]


[42, 37, 23]
[-4, 1, 2]

In [17]:
# providing an alternate sort key to nlargest/nsmallest
portfolio = [
{'name': 'IBM', 'shares': 100, 'price': 91.1},
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
{'name': 'FB', 'shares': 200, 'price': 21.09},
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
{'name': 'ACME', 'shares': 75, 'price': 115.65}
]

heapq.nsmallest(3, portfolio, key=lambda s: s['price'])


Out[17]:
[{'name': 'YHOO', 'price': 16.35, 'shares': 45},
 {'name': 'FB', 'price': 21.09, 'shares': 200},
 {'name': 'HPQ', 'price': 31.75, 'shares': 35}]

3. Counter


In [1]:
words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]

from collections import Counter
word_counts = Counter(words)  #Works with any hashable items, not just strings!
word_counts.most_common(3)


Out[1]:
[('eyes', 8), ('the', 5), ('look', 4)]

In [4]:
morewords = ['why','are','you','not','looking','in','my','eyes']
for word in morewords:
    word_counts[word] += 1
word_counts.most_common(3)


Out[4]:
[('eyes', 11), ('my', 6), ('the', 5)]

In [5]:
evenmorewords = ['seriously','look','into','them','while','i','look','at', 'you']
word_counts.update(evenmorewords)
word_counts.most_common(3)


Out[5]:
[('eyes', 11), ('look', 6), ('my', 6)]

In [6]:
a = Counter(words)
b = Counter(morewords)
c = Counter(evenmorewords)

In [7]:
# combine counters
d = b + c
d


Out[7]:
Counter({'are': 1,
         'at': 1,
         'eyes': 1,
         'i': 1,
         'in': 1,
         'into': 1,
         'look': 2,
         'looking': 1,
         'my': 1,
         'not': 1,
         'seriously': 1,
         'them': 1,
         'while': 1,
         'why': 1,
         'you': 2})

In [8]:
# subtract counts
e = a-d
e


Out[8]:
Counter({'around': 2,
         "don't": 1,
         'eyes': 7,
         'into': 2,
         'look': 2,
         'my': 2,
         'the': 5,
         'under': 1,
         "you're": 1})

In [ ]: